package cn.imvc.app.controllers.member;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;

import cn.imvc.app.controllers.goods.GoodsControllerImpl;
import cn.imvc.app.entities.Dealer;
import cn.imvc.app.entities.DealerLevel;
import cn.imvc.app.entities.PointsToMoney;
import cn.imvc.app.models.DealerLevelModel;
import cn.imvc.app.models.LoggingModel;
import cn.imvc.app.models.PointsToMoneyModel;
import cn.imvc.core.DialogBean;
import cn.imvc.core.Render;
import cn.imvc.data.domain.ViewRequest;

public class PointsToMoneyControllerImpl implements PointsToMoneyController{
	static Logger logger = Logger.getLogger(PointsToMoneyControllerImpl.class);
	public Page<PointsToMoney> page;
	public ViewRequest viewRequest;
	public PointsToMoney pointsToMoney;
	public int id;
	public int thePercent;
	public Map<Integer, String> dealerLevelMap;
	private List<DealerLevel> dealerLevelList;
	@Autowired
	private DealerLevelModel dealerLevelModel;
	@Autowired
	PointsToMoneyModel pointsToMoneyModel;
	@Autowired
	LoggingModel loggingModel;
	public String search() {
		return null;
	}

	public String index() {
		dealerLevelMap = new HashMap<>();
		List<DealerLevel> dealerLevels = dealerLevelModel.findAll();
		Iterator<DealerLevel> iterator =dealerLevels.iterator();
		while (iterator.hasNext()) {
			DealerLevel dealerLevel = (DealerLevel) iterator.next();
			dealerLevelMap.put(dealerLevel.getLevel(), dealerLevel.getName());
		}
		if(viewRequest==null){
			viewRequest = new ViewRequest();
		}
		page = pointsToMoneyModel.find(viewRequest);
		return "points-to-money/index";
	}
	public String show() {
		return null;
	}

	public String editNew() {
		dealerLevelList = dealerLevelModel.findAll();
		return "points-to-money/edit";
	}
   
	public String create() {
		pointsToMoney.setPercent(thePercent*100);
		pointsToMoneyModel.save(pointsToMoney);
		loggingModel.info(logger,"添加积分兑换："+pointsToMoney.getName());
		return Render.json(new DialogBean("200","添加成功","","closeCurrent"));
	}

	public String edit() {
		pointsToMoney = pointsToMoneyModel.get(id);
		dealerLevelList = dealerLevelModel.findAll();
		return "points-to-money/edit";
	}

	public String update() {
		pointsToMoney.setPercent(thePercent*100);
		pointsToMoneyModel.update(id,pointsToMoney);
		loggingModel.info(logger,"修改积分兑换："+pointsToMoney.getName());
		return Render.json(new DialogBean("200","修改成功","","closeCurrent"));
	}

	public String destroy() {
		return null;
	}

	public String destroyBatch() {
		return null;
	}

	public ViewRequest getViewRequest() {
		return viewRequest;
	}

	public void setViewRequest(ViewRequest viewRequest) {
		this.viewRequest = viewRequest;
	}

	public PointsToMoney getPointsToMoney() {
		return pointsToMoney;
	}

	public void setPointsToMoney(PointsToMoney pointsToMoney) {
		this.pointsToMoney = pointsToMoney;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public int getThePercent() {
		return thePercent;
	}

	public void setThePercent(int thePercent) {
		this.thePercent = thePercent;
	}

	public List<DealerLevel> getDealerLevelList() {
		return dealerLevelList;
	}

	public void setDealerLevelList(List<DealerLevel> dealerLevelList) {
		this.dealerLevelList = dealerLevelList;
	}

}
